package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.core.controller.AbstractCoreController;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @version 1.0
 * @description <p>自媒体图文内容信息</p>
 * @package com.itheima.wemedia.controller
 */
@Api(value = "WmNewsController", tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService = wmNewsService;
    }


    /**
     * 文章内容发布
     *
     * @param dto
     * @param draft
     * @return
     */
    @ApiOperation(value = "文章内容发布")
    @PostMapping("/submit")
    public ResultVo submit(@RequestBody WmNewsDtoSave dto, Boolean draft) {
        Long wmNewId = wmNewsService.submit(dto, draft);
        return ResultVo.ok(wmNewId);
    }

    /**
     * 文章内容分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "文章内容分页查询")
    @PostMapping("/list")
    public PageResultVo listAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.listPage(dto);

    }

    @ApiOperation(value = "根据id查询文章内容")
    @GetMapping("/one/{wmNewsId}")
    public ResultVo findById(@PathVariable("wmNewsId") Long wmNewsId) {
        WmNews wmNews = wmNewsService.getById(wmNewsId);
        return ResultVo.ok(wmNews);
    }

    /**
     * 通过id删除文章
     *
     * @param wmNewsId
     * @return
     */
    @ApiOperation("根据id删除文章")
    @GetMapping("/del_news/{wmNewsId}")
    public ResultVo deleteById(@PathVariable(value = "wmNewsId") Long wmNewsId) {
        wmNewsService.deleteById(wmNewsId);
        return ResultVo.ok();
    }

    @ApiOperation("文章上下架")
    @PostMapping("/down_or_up")
    public ResultVo downOrUp(@RequestBody Map<String, Integer> map) {

        wmNewsService.downOrUp(map);
        return ResultVo.ok();
    }
}

